package com.lkd.handler;

import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.VmStatusContract109;
import com.lkd.emq.Topic;
import com.lkd.feign.VMService;
import com.lkd.http.vo.TaskViewModel;
import com.lkd.service.TaskService;
import com.lkd.utils.JsonUtil;
import com.lkd.vo.VmVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * @author brianxia
 * @version 1.0
 * @date 2022-12-03 11:51
 */
@Component
@Topic(TopicConfig.VMS_STATUS_TOPIC)
@Slf4j
public class VmStatusHandler implements MsgHandler {

    @Autowired
    private TaskService taskService;
    @Autowired
    private VMService vmService;

    @Override
    public void process(String jsonMsg) throws IOException {
        //1.接收消息协议
        if(!StringUtils.isEmpty(jsonMsg)){
            VmStatusContract109 contract = JsonUtil.getByJson(jsonMsg, VmStatusContract109.class);

            if(StringUtils.isEmpty(contract.getInnerCode())
                    || CollectionUtils.isEmpty(contract.getStatusInfo())){
                log.error("消息协议接收失败，消息内容：" + jsonMsg);
                return;
            }

            //2.判断售货机是否有问题
            List<VmStatusContract109.StatusInfoDTO> statusInfo = contract.getStatusInfo();

//            boolean flag = true;
//            for (VmStatusContract109.StatusInfoDTO statusInfoDTO : statusInfo) {
//                if(Objects.equals(statusInfoDTO.getStatus(),false)){
//                    flag = false;
//                    break;
//                }
//            }
            //判断是否有一个部件状态为false
            if( statusInfo.stream().anyMatch(x -> Objects.equals(x.getStatus(),false))){

                TaskViewModel taskViewModel = new TaskViewModel();
                //设置创建类型为自动0
                taskViewModel.setCreateType(VMSystem.TASK_CREATE_TYPE_AUTO);
                //设置售货机编号
                taskViewModel.setInnerCode(contract.getInnerCode());
                //获取售货机信息
                VmVO vmInfo = vmService.getVMInfo(contract.getInnerCode());
                if(vmInfo == null){
                    log.error("售货机不存在" + contract.getInnerCode());
                    return;
                }
                //设置任务执行人
                Integer leastTaskUser = taskService.getLeastTaskUser(vmInfo.getRegionId(), true);
                if(leastTaskUser == null){
                    log.error("最少工单运维人员不存在，区域：" + vmInfo.getRegionId());
                    return;
                }
                taskViewModel.setUserId(leastTaskUser);
                //设置默认分配人
                taskViewModel.setAssignorId(0);
                //设置工单类型为维修
                taskViewModel.setProductType(VMSystem.TASK_TYPE_REPAIR);
                //描述信息
                taskViewModel.setDesc(jsonMsg);
                //创建自动工单
                taskService.create(taskViewModel);
            }


        }
    }
}
